<template>
    <view class="order-page">
        <view class="order-form-container">
            <view class="order-form">
                <!-- 客户编号 -->
                <view class="form-item">
                    <view>编号：</view>
                    <view>{{ customer.id || '无' }}</view>
                </view>

                <!-- 客户名称 -->
                <view class="form-item">
                    <view>客户名称：</view>
                    <uni-easyinput v-model="customer.name" placeholder="请输入客户名称" />
                </view>

                <!-- 负责人的用户编号 -->
                <view class="form-item">
                    <view>负责人：</view>
                    <uni-easyinput v-model="customer.ownerUserId" placeholder="请输入负责人的用户编号" />
                </view>

                <!-- 手机 -->
                <view class="form-item">
                    <view>手机：</view>
                    <uni-easyinput v-model="customer.mobile" placeholder="请输入手机号码" />
                </view>

                <!-- 电话 -->
                <view class="form-item">
                    <view>电话：</view>
                    <uni-easyinput v-model="customer.telephone" placeholder="请输入电话号码" />
                </view>

                <!-- QQ -->
                <view class="form-item">
                    <view>QQ：</view>
                    <uni-easyinput v-model="customer.qq" placeholder="请输入QQ号" />
                </view>

                <!-- 微信 -->
                <view class="form-item">
                    <view>微信：</view>
                    <uni-easyinput v-model="customer.wechat" placeholder="请输入微信号" />
                </view>

                <!-- 邮箱 -->
                <view class="form-item">
                    <view>邮箱：</view>
                    <uni-easyinput v-model="customer.email" placeholder="请输入邮箱" />
                </view>

                <!-- 地区编号 -->
                <view class="form-item">
                    <view>地区编号：</view>
                    <uni-easyinput v-model.number="customer.areaId" placeholder="请输入地区编号" />
                </view>

                <!-- 详细地址 -->
                <view class="form-item">
                    <view>详细地址：</view>
                    <uni-easyinput v-model="customer.detailAddress" placeholder="请输入详细地址" />
                </view>

                <!-- 客户来源 -->
                <view class="form-item">
                    <view>客户来源：</view>
                    <picker mode="selector" :range="sourceNames" @change="onSourceChange" :value="sourceIndex">
                        <view class="picker">{{ sourceNames[sourceIndex] || '请选择客户来源' }}</view>
                    </picker>
                </view>

                <!-- 备注 -->
                <view class="form-item">
                    <view>备注：</view>
                    <uni-easyinput type="textarea" v-model="customer.remark" placeholder="请输入备注" />
                </view>

                <!-- 提交按钮 -->
                <view class="form-item">
                    <button type="button" @click="submitForm">{{ isEdit ? '修改' : '新增' }}</button>
                </view>
            </view>
        </view>
    </view>
</template>
 <script>
import {
    getCustomer,
    createCustomer,
    updateCustomer, 
} from "@/sheep/api/erp/customer.js";
 import DictApi from "@/sheep/api/system/dict.js";

export default {
    data() {
        return {
            customerId: "", // 从路由参数获取客户ID
            customer: {
                id: null,
                name: '',
                ownerUserId: null,
                mobile: '',
                telephone: '',
                qq: '',
                wechat: '',
                email: '',
                areaId: null,
                detailAddress: '',
                source: null,
                remark: '',
            }, // 用于存储客户详情
            isEdit: false, // 是否为编辑模式
            sourceIds: [], // 客户来源列表
            sourceNames: [], // 客户来源名称列表
            sourceIndex: -1, // 选择的客户来源索引
        };
    },
    methods: {
		setPageTitle() {
		      const title = this.isEdit ? "修改客户" : "新增客户";
		      uni.setNavigationBarTitle({
		        title,
		      });
		    },
        async fetchCustomerDetails() {
            if (!this.customerId) return; // 如果没有 customerId，表示是新增
            try {
                const res = await getCustomer(this.customerId);
                this.customer = res.data;
                this.isEdit = true;
                this.sourceIndex = this.sourceIds.findIndex(item => item.value == this.customer.source);
            } catch {
                uni.showToast({
                    title: "获取客户详情失败",
                    icon: "none"
                });
            }
        },
        async fetchSourceList() {
            try {
				 const res = await DictApi.getDictLists("crm_customer_source"); 
            //    const res = await getSourceList();
                this.sourceIds = res.data; // 获取来源数据
                this.sourceNames = res.data.map(item => item.label); // 提取来源名称
                if (this.isEdit) {
                    this.sourceIndex = this.sourceIds.findIndex(item => item.value == this.customer.source);
                }
            } catch {
                uni.showToast({
                    title: "获取客户来源失败",
                    icon: "none"
                });
            }
        },
        onSourceChange(e) {
            this.sourceIndex = e.detail.value;
            if (this.sourceIndex !== -1) {
                this.customer.source = this.sourceIds[this.sourceIndex].id;
            }
        },
        submitForm() {
            if (this.isEdit) {
                this.updateCustomer();
            } else {
                this.createCustomer();
            }
        },
        createCustomer() {
            createCustomer(this.customer)
                .then(() => {
                    uni.showToast({
                        title: "新增成功",
                        icon: "success"
                    });
                    uni.navigateBack();
                })
                .catch(err => {
                    uni.showToast({
                        title: err.msg || "新增失败",
                        icon: "none"
                    });
                });
        },
        updateCustomer() {
            updateCustomer(this.customer).then(() => {
                    uni.showToast({
                        title: "修改成功",
                        icon: "success"
                    });
                    uni.navigateBack();
                })
                .catch(err => {
                    uni.showToast({
                        title: err.msg || "修改失败",
                        icon: "none"
                    });
                });
        },
    },
    onLoad() {
        this.customerId = uni.getStorageSync('customerId') || ''; // 确保 customerId 总是有值
		this.isEdit = !!this.customerId; // 如果有 productId，则为编辑模式
		this.setPageTitle(); // 设置页面标题
        this.fetchSourceList();
        this.fetchCustomerDetails();
    },
};
</script>
 